/**
 * 建筑管理器
 * 负责建筑的升级、解锁和效率管理
 */

import { GAME_CONFIG } from '../config/game-config.js';

export class BuildingManager {
    constructor(gameData, resourceManager) {
        this.gameData = gameData;
        this.resourceManager = resourceManager;
    }

    /**
     * 获取建筑信息
     * @param {string} buildingId - 建筑ID
     * @returns {Object|null} 建筑信息
     */
    getBuilding(buildingId) {
        return this.gameData.buildings.find(b => b.id === buildingId) || null;
    }

    /**
     * 获取所有建筑
     * @returns {Array} 建筑列表
     */
    getAllBuildings() {
        return this.gameData.buildings;
    }

    /**
     * 获取已解锁的建筑
     * @returns {Array} 已解锁的建筑列表
     */
    getUnlockedBuildings() {
        return this.gameData.buildings.filter(b => b.unlocked);
    }

    /**
     * 检查建筑是否可以升级
     * @param {string} buildingId - 建筑ID
     * @returns {Object} {canUpgrade: boolean, reason: string}
     */
    canUpgradeBuilding(buildingId) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return { canUpgrade: false, reason: '建筑不存在' };
        }

        if (!building.unlocked) {
            return { canUpgrade: false, reason: '建筑未解锁' };
        }

        if (!this.resourceManager.hasEnoughResources(building.upgradeCost)) {
            return { canUpgrade: false, reason: '资源不足' };
        }

        return { canUpgrade: true, reason: '' };
    }

    /**
     * 升级建筑
     * @param {string} buildingId - 建筑ID
     * @returns {Object} {success: boolean, message: string, building: Object}
     */
    upgradeBuilding(buildingId) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return { success: false, message: '建筑不存在' };
        }

        const canUpgrade = this.canUpgradeBuilding(buildingId);
        if (!canUpgrade.canUpgrade) {
            return { success: false, message: canUpgrade.reason };
        }

        // 扣除资源
        if (!this.resourceManager.deductResources(building.upgradeCost)) {
            return { success: false, message: '扣除资源失败' };
        }

        // 升级建筑
        building.level += 1;

        // 增加效率
        building.efficiency += GAME_CONFIG.BUILDINGS.EFFICIENCY_GROWTH;

        // 调整升级成本
        const multiplier = GAME_CONFIG.BUILDINGS.UPGRADE_COST_MULTIPLIER;
        building.upgradeCost.wood = Math.floor(building.upgradeCost.wood * multiplier);
        building.upgradeCost.food = Math.floor(building.upgradeCost.food * multiplier);

        // 特殊建筑效果
        this.applyBuildingSpecialEffect(buildingId, building);

        return {
            success: true,
            message: `成功升级 ${building.name} 到 ${building.level} 级`,
            building: building
        };
    }

    /**
     * 检查建筑是否可以解锁
     * @param {string} buildingId - 建筑ID
     * @returns {Object} {canUnlock: boolean, reason: string}
     */
    canUnlockBuilding(buildingId) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return { canUnlock: false, reason: '建筑不存在' };
        }

        if (building.unlocked) {
            return { canUnlock: false, reason: '建筑已解锁' };
        }

        if (!building.unlockCost) {
            return { canUnlock: false, reason: '该建筑无法解锁' };
        }

        if (!this.resourceManager.hasEnoughResources(building.unlockCost)) {
            return { canUnlock: false, reason: '资源不足' };
        }

        return { canUnlock: true, reason: '' };
    }

    /**
     * 解锁建筑
     * @param {string} buildingId - 建筑ID
     * @returns {Object} {success: boolean, message: string, building: Object}
     */
    unlockBuilding(buildingId) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return { success: false, message: '建筑不存在' };
        }

        const canUnlock = this.canUnlockBuilding(buildingId);
        if (!canUnlock.canUnlock) {
            return { success: false, message: canUnlock.reason };
        }

        // 扣除资源
        if (!this.resourceManager.deductResources(building.unlockCost)) {
            return { success: false, message: '扣除资源失败' };
        }

        // 解锁建筑
        building.unlocked = true;

        return {
            success: true,
            message: `成功解锁 ${building.name}`,
            building: building
        };
    }

    /**
     * 应用建筑特殊效果
     * @param {string} buildingId - 建筑ID
     * @param {Object} building - 建筑对象
     */
    applyBuildingSpecialEffect(buildingId, building) {
        if (buildingId === 'house') {
            // 民房升级增加人口上限
            this.resourceManager.increaseMaxPopulation(
                GAME_CONFIG.BUILDINGS.HOUSE_POPULATION_INCREASE
            );
        }
    }

    /**
     * 修改建筑效率
     * @param {string} buildingId - 建筑ID
     * @param {number} efficiencyChange - 效率变化值（可以是正数或负数）
     * @returns {boolean} 是否成功
     */
    modifyBuildingEfficiency(buildingId, efficiencyChange) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return false;
        }

        building.efficiency += efficiencyChange;
        building.efficiency = Math.max(0.1, building.efficiency); // 最低效率10%

        return true;
    }

    /**
     * 获取建筑显示信息
     * @param {string} buildingId - 建筑ID
     * @returns {Object|null} 建筑显示信息
     */
    getBuildingDisplayInfo(buildingId) {
        const building = this.getBuilding(buildingId);

        if (!building) {
            return null;
        }

        const canUpgrade = building.unlocked ? this.canUpgradeBuilding(buildingId) : null;
        const canUnlock = !building.unlocked ? this.canUnlockBuilding(buildingId) : null;

        return {
            ...building,
            canUpgrade: canUpgrade ? canUpgrade.canUpgrade : false,
            canUnlock: canUnlock ? canUnlock.canUnlock : false,
            icon: GAME_CONFIG.ICONS[buildingId] || GAME_CONFIG.ICONS.default
        };
    }

    /**
     * 获取所有建筑的显示信息
     * @returns {Array} 建筑显示信息列表
     */
    getAllBuildingsDisplayInfo() {
        return this.gameData.buildings.map(building =>
            this.getBuildingDisplayInfo(building.id)
        );
    }

    /**
     * 获取可见的建筑（已解锁或可解锁）
     * @returns {Array} 可见建筑列表
     */
    getVisibleBuildings() {
        return this.gameData.buildings.filter(b => b.unlocked || b.unlockCost);
    }
}
